package me.andy.superappsorganizer.control;

import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings.Secure;
import android.util.Log;
import android.widget.Toast;

import com.gc.android.market.api.MarketSession;
import com.gc.android.market.api.MarketSession.Callback;
import com.gc.android.market.api.model.Market.AppsRequest;
import com.gc.android.market.api.model.Market.AppsResponse;
import com.gc.android.market.api.model.Market.ResponseContext;

public class MarketDataRetriever {

	private String sPackage;
	//private ContentResolver contentResolver;
	private Context m_context;
	String category;
	
	public MarketDataRetriever(Context context, String sPackage)
	{
		this.m_context = context;
		this.sPackage = sPackage;
	}
	
	public String retrieveCategory()
	{
		

		MarketSession session = new MarketSession();
		String androidId = null; //=Secure.getString(m_context.getContentResolver(), Secure.ANDROID_ID);
		if (androidId==null) {
			androidId="3000000000000000";
		}
		session.login("andydev@gmail.com","ch!6319l30");
		session.getContext().setAndroidId(androidId);
		session.getContext().setDeviceAndSdkVersion("crespo:7");

		String query = "pname:" + sPackage; //"pname:com.sas.basketball";
		AppsRequest appsRequest = AppsRequest.newBuilder()
				.setQuery(query)
				.setWithExtendedInfo(true)
				.setStartIndex(0).setEntriesCount(10)
				.build();

		session.append(appsRequest, new Callback<AppsResponse>() {
			@Override
			public void onResult(ResponseContext context, AppsResponse response) {
				if (response.getAppCount() > 0)
				{
					category = response.getApp(0).getExtendedInfo().getCategory().toString();
					Log.i("ANDY",response.getApp(0).getExtendedInfo().getCategory().toString());
				} else {
					category = "Not found";
					Log.i("ANDY","Not Found");
				}

			}
		});
		session.flush();
		return category;
	}
}
